package biz.impl;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import biz.MissionBiz;
import dao.MissionsDao;
import dao.ProjectsDao;
import pojo.MissionRelations;
import pojo.Missions;
import pojo.ProjectMissions;
import pojo.Projects;
import pojo.Teams;
import pojo.UserRoleProject;
import util.TestImag;

/**
 * @author WuYuan
 * 
 */
public class MissionBizImpl implements MissionBiz {

	private ProjectsDao projectsDao;

	private MissionsDao missionsDao;

	@Override
	public List<Missions> findMissionsByUrpid(String projectid, String userid, String roleId) {
		try {
			return missionsDao.findMissionsByUrpid(projectid, userid, roleId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updateCompleteMission(String missionid) {
		System.out.println(missionid);
		try {
			missionsDao.updateMissionsToCompleted(missionid);
			List<Missions> allmissions= missionsDao.findAllMissionsByMissionid(missionid);
			boolean completedmission=true;
			for(int i=0;i<allmissions.size();i++){
				if("0".equals(allmissions.get(i).getIscompleted())){
					completedmission=false;
					break;
				}
			}
			if(completedmission){
				System.out.println("完成阶段任务");
				missionsDao.updateProjectMissionsToCompleted(missionid);
			}else{
				System.out.println("因存在未完成任务,阶段任务没有完成");
			}
			
			Missions mission = missionsDao.findMissionsByMissionid(missionid);
			long time1= mission.getEtc().getTime();
			long time2= mission.getAft().getTime();
			System.out.println(time1);
			System.out.println(time2);
			if((time1-time2)<0){
				missionsDao.updateObjectivescore(missionid);
			}
			return 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<Projects> findProjectsByUrpid(String teamid, String userid) {
		try {
			return projectsDao.findProjectsByUrpid(teamid, userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public BufferedImage findMissionRelationByUrpid(String projectid, String roleid) {
		try {
			List<MissionRelations> missionRelations = new ArrayList<>();
			List<ProjectMissions> projectMissions = new ArrayList<>();
			Map<String, String> missionrelationmap = new HashMap<String, String>();
			Map<String, String> pmStatus = new HashMap<String, String>();
			missionRelations = missionsDao.findPMRelationsByUrpid(projectid, roleid);
			projectMissions = missionsDao.findProjectMissionsByUrpid(projectid, roleid);
			if (!missionRelations.isEmpty()) {
				for (int i = 0; i < missionRelations.size(); i++) {
					missionrelationmap.put(missionRelations.get(i).getFrontMissionName(),
							missionRelations.get(i).getAfterMissionName());
				}
			}
			
			if(projectMissions.isEmpty()){
				System.out.println("没有查询到projectmissions");
			}else{
				for(int i=0;i<projectMissions.size();i++){
					System.out.println("阶段项目名称"+projectMissions.get(i).getMissionName()+"阶段项目是否完成"+ projectMissions.get(i).getIscompleted());
					pmStatus.put(projectMissions.get(i).getMissionName(), projectMissions.get(i).getIscompleted());
				}
			}
			// ImageIO.write(image, "jpg", new File("F:/"+name+".jpg"));
			return TestImag.dopaint(missionrelationmap, missionRelations,pmStatus);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	};

	public ProjectsDao getProjectsDao() {
		return projectsDao;
	}

	public void setProjectsDao(ProjectsDao projectsDao) {
		this.projectsDao = projectsDao;
	}

	public MissionsDao getMissionsDao() {
		return missionsDao;
	}

	public void setMissionsDao(MissionsDao missionsDao) {
		this.missionsDao = missionsDao;
	}

	@Override
	public List<Teams> findTeamsByUrpid(String userid) {
		// TODO Auto-generated method stub
		try {
			System.out.println("userid="+userid);
			return missionsDao.findTeamsByUrpid(userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public UserRoleProject findRoleidByUrpid(String projectid, String userid) {
		// TODO Auto-generated method stub
		try {
			return projectsDao.findRoleidByUrpid(projectid, userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public double findMemberScore(String teamid, String userid) {
		try {
			List<UserRoleProject> urplist = projectsDao.findMemberByTandU(teamid, userid);
			double score = 0.0;
			if(!urplist.isEmpty()){
				for(int i=0;i<urplist.size();i++){
					score=score+urplist.get(i).getSubjectiveScore()+urplist.get(i).getSubjectiveScore();
				}
				return score/(urplist.size());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
